package app.project.streetlensproject.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;
import app.project.streetlensproject.HomeActivity;
import app.project.streetlensproject.MyApplication;
import app.project.streetlensproject.R;
import app.project.streetlensproject.adapter.CategoryAdapter;
import app.project.streetlensproject.model.Category;
import app.project.streetlensproject.serverapi.ServerAPI;
import app.project.streetlensproject.util.AsyncTask;
import app.project.streetlensproject.util.CommonUtils;
import app.project.streetlensproject.util.Constants;
import app.project.streetlensproject.util.DialogUtils;
import app.project.streetlensproject.util.GetDataTask;
import app.project.streetlensproject.util.IGetDataTask;
import app.project.streetlensproject.util.StringUtils;

public class FgRegister extends Fragment implements OnClickListener {
	private Button btnRegister, btnCancel;
	private EditText edtUser, edtPass, edtEmail, edtRePass;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fg_register, container, false);

		btnRegister = (Button) v.findViewById(R.id.btn_register);
		btnCancel = (Button) v.findViewById(R.id.btn_cancel);
		edtEmail = (EditText) v.findViewById(R.id.edt_email);
		edtPass = (EditText) v.findViewById(R.id.edt_pass);
		edtRePass = (EditText) v.findViewById(R.id.edt_repass);
		edtUser = (EditText) v.findViewById(R.id.edt_user_name);

		btnRegister.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		return v;
	}

	public void onClick(View v) {
		final String email = edtEmail.getText().toString().trim();
		final String pass = edtPass.getText().toString().trim();
		String repass = edtRePass.getText().toString().trim();
		if (!pass.equals(repass)) {
			DialogUtils.showInfoDialog(getActivity(), null, getString(R.string.new_pass_not_match), null);
			return;
		}
		int id = v.getId();
		if (id == R.id.btn_register) {
			if (!StringUtils.vaildEmail(email)) {
				Toast.makeText(getActivity(), R.string.email_not_vaild, Toast.LENGTH_LONG).show();
				return;
			}
			if (pass.length() < 6) {
				Toast.makeText(getActivity(), R.string.pass_not_vaild, Toast.LENGTH_LONG).show();
				return;
			}
			new GetDataTask(getActivity(), new IGetDataTask() {
				@Override
				public void onPostExecute(boolean result) throws Exception {
					super.onPostExecute(result);
					if (result) {
						CommonUtils.saveStringSetting(MyApplication.getAppContext(), Constants.USER_NAME, edtUser
								.getText().toString().trim());
						HomeActivity activity = ((HomeActivity) getActivity());
						activity.tvUserName.setText(getString(R.string.hello) + " "
								+ edtUser.getText().toString().trim());
						Toast.makeText(getActivity(), "Đăng ký thành công", Toast.LENGTH_LONG).show();
						activity.gotoCategory();
						activity.isLogin = true;
					} else
						DialogUtils.showInfoDialog(getActivity(), null, "Đăng ký lỗi", null);
				}

				@Override
				public boolean doInBackGround() throws Exception {
					return ServerAPI.registerUser(getActivity(), edtUser.getText().toString().trim(), edtPass.getText()
							.toString().trim(), email);
				}
			}, true).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else if (id == R.id.btn_cancel) {
			((HomeActivity) getActivity()).gotoCategory();
		}
	}

}
